import { Platform, SendMessageType } from "@/core/config/DeclareGlobal";
import wsClient from "@/core/communication/WsService";

/**
 * 发送消息到Unity的类
 * @class SendToUnity
 */
class SendToUnity {
  /**
   * @private
   * @type {HTMLIFrameElement | undefined}
   */
  #iframeDom = undefined;

  constructor() {}

  /**
   * 初始化SendToUnity实例
   * @function init
   * @param {HTMLIFrameElement} iframeDom - Unity的iframe元素
   * @returns {void}
   */
  init(iframeDom) {
    this.#iframeDom = iframeDom;
  }

  /**
   * 发送消息到Unity
   * @function send
   * @param {Object} message - 要发送给Unity的消息数据
   * @param {string} message.type - 消息类型,SendMessageType中的一个值
   * @param {*} message.data - 消息数据
   * @param {string} [message.platform] - 平台标识（会被覆盖为web2unity）
   * @returns {void}
   */
  send(message) {
    message.platform = Platform.web2unity;

    if (this.#iframeDom && this.#iframeDom.contentWindow) {
      this.#iframeDom.contentWindow.postMessage(message, "*");
    }

    // wsClient.sendPairingMessage(JSON.stringify(message));
  }
}

// 创建单例实例
const sendToUnity = new SendToUnity();
export default sendToUnity;
